<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="UTF-8">
		<script src="js/jquery-1.9.1.min.js"></script>
		<script src="js/json2.js"></script>
		<script>
			$(document).ready(function() {
				// 初始化数据
				$.ajax({
					type: "GET",
					url: "/SpringDemo/good/list",
					dataType : 'json',
					contentType:"application/json",
					success: function(data, textStatus) {
						if (data && data.content) {
							$.each(data.content, function(index, good){
								$("#goodList").append("<li>" + good.goodName + "</li>");
							});
							$("#log").val($("#log").val() + "Load data success!! - " + JSON.stringify(data) + "\r\n");	
						}
					},
					error: function(XMLHttpRequest, textStatus, errorThrown){
						$("#log").val($("#log").val() + "Load data failure!! - " + errorThrown + "\r\n");
					}
				});
				
				// 所有订单
				$("#allGoods").click(function() {
					$.ajax({
						type: "GET",
						url: "/SpringDemo/good/listAll",
						dataType : 'json',
						contentType:"application/json",
						success: function(data, textStatus) {
							if (data && data.content) {
								$("#log").val($("#log").val() + "Load all goods success!! - " + JSON.stringify(data) + "\r\n");	
							}
						},
						error: function(XMLHttpRequest, textStatus, errorThrown){
							$("#log").val($("#log").val() + "Load all goods failure!! - " + errorThrown + "\r\n");
						}
					});
				});
				
				// 购买按钮
				$("#orderBtn").click(function() {
					var param = {
						"goodName" : $("#goodName").val(),
						"count" : $("#count").val()
					};
					$.ajax({
						type: "POST",
						url: "/SpringDemo/good/add",
						data: JSON.stringify(param),
						dataType : 'json',
						contentType:"application/json",
						success: function(data, textStatus) {
							if (data && data.statusCode == 0) {
								$("#goodList").append("<li>" + $("#goodName").val() + "</li>");	
							}
							$("#log").val($("#log").val() + JSON.stringify(data) + "\r\n");	
						},
						error: function(XMLHttpRequest, textStatus, errorThrown){
							$("#log").val($("#log").val() + "Add failure!! - " + errorThrown + "\r\n");
						}
					});
				});
			});
		</script>
	</head>
	<body>
		<p id="header">
			<span>SpringDemo - 您好 <b><sec:authentication property="name"/></b> 欢迎购物</span>
			<span>
				<a id="allGoods" href="#">所有订单</a>
			</span>
			<span>
				<a href="/SpringDemo/user/list">所有用户</a>
			</span>
			<span>
				<a href="/SpringDemo/logout">Logout</a>
			</span>
		</p>
		<div style="width:800px">
			<p>
				<fieldset>
		    		<legend>购买商品</legend>
					<p>商品：<input id="goodName" type="text" name="goodName"/></p>
					<p>数量：<input id="count" type="number" name="count" min="1" max="10"/></p>
					<p><input id="orderBtn" type="button" value="购买"/></p>
				</fieldset>
			</p>
			<p>
				<fieldset>
		    		<legend>我的商品</legend>
					<ul id="goodList">
					</ul>
				</fieldset>
			</p>
			
			<p>
				日志：
				<textarea id="log" rows="8" cols="10" style="width:99%;overflow:auto;word-break:break-all;"></textarea>
			</p>
		</div>
	</body>
</html>